package com.renwei.yunlong.service;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;

import com.renwei.yunlong.event.FileExistEvent;
import com.renwei.yunlong.utils.StringUtils;

import java.net.HttpURLConnection;
import java.net.URL;

import de.greenrobot.event.EventBus;

/**
 * Project :  yunna.
 * Package name: com.renwei.yunlong.service
 * Created by :  benjamin.
 * Created time: 2018/4/25 16:24
 * Changed by :  benjamin.
 * Changed time: 2018/4/25 16:24
 * Class description:
 */

public class FileExistService extends IntentService {
    String path;

    public FileExistService() {
        super("FileExistService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        path = StringUtils.value(intent.getStringExtra("path"));

        try {
            //文件路径   可远程  可本地
            URL url = new URL(path);
            //连接访问
            HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
            System.out.println("url=" + url);
            Long TotalSize = Long.parseLong(urlcon.getHeaderField("Content-Length"));
            System.out.println("TotalSize=" + TotalSize);
            if (TotalSize != 1308) {
                EventBus.getDefault().post(new FileExistEvent(true, StringUtils.value(path)));
                System.out.println("存在");
            } else {
                EventBus.getDefault().post(new FileExistEvent(false, StringUtils.value(path)));
                System.out.println("不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            EventBus.getDefault().post(new FileExistEvent(false, StringUtils.value(path)));
        }


    }
}
